home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c-part1 / 8431 < prev    next >
Encoding:
Internet Message Format  |  1996-08-05  |  6.8 KB

  1. Path: keats.ugrad.cs.ubc.ca!not-for-mail
  2. From: c2a192@ugrad.cs.ubc.ca (Kazimir Kylheku)
  3. Newsgroups: comp.lang.c
  4. Subject: Re: What is &Variable (declared as: char Variable[10])?
  5. Date: 3 Mar 1996 21:36:09 -0800
  6. Organization: Computer Science, University of B.C., Vancouver, B.C., Canada
  7. Message-ID: <4hdvg9INN71j@keats.ugrad.cs.ubc.ca>
  8. References: <4gqpa1$3h9@alcor.usc.edu> <4hagqg$6je@daily-planet.execpc.com>
  9. NNTP-Posting-Host: keats.ugrad.cs.ubc.ca
  10.  
  11. In article <4hagqg$6je@daily-planet.execpc.com>,
  12. Reginald W. Sprecher <sprecher@execpc.com> wrote:
  13.  >I am sure that many of you will protest about what I am about to say
  14.  >about pointers but it is my intention to try and clarify what all the
  15.  >fuss is about.
  16.  >
  17.  >First off we need to start with some definitions.
  18.  >
  19.  >1.  A pointer - a pointer is a variable which has as its purpose the
  20.  >ability to point to another variable.  One thing that needs to be made
  21.  >clear here is that the size of a pointer is always the same no matter
  22.  >what type of data the pointer is pointing at ( a pointer to a char and
  23.  >an int are the same size). However, this is not and will not hold
  24.  
  25. This is false. Read the FAQ about some unusual computer architectures which
  26. have been used as C targets.
  27.  
  28.  >across machines.  For example, a pointer on an pc-xt type computer
  29.  >would be different from a pentium computer.  This is because the
  30.  
  31. You are botching up your definition of a pointer with secondary issues. :)
  32. If you said this to a class, it would distract from the lecture about pointers.
  33.  
  34.  >pointer needs to be able to hold an address to something.  If the
  35.  >memory space in a target machine is 16 bits then a pointer for that
  36.  >machine is 16 bits.  If the memory space on a different machine is 32
  37.  >bits then the size of the pointer is 32 bits.  
  38.  >
  39.  >2. address - all variables, including pointers and arrays, have a
  40.  >address as to where they live in memory.
  41.  
  42. Even variables declared 'register'? Gotcha. Gotta be careful around here,
  43. it's shark territory! :) What about bit-field variables? Gotcha twice.
  44.  
  45.  >3. & - the & is the 'give me the address where the indicated variable
  46.  >lives in memory' command.  All variables live in memory and therefore
  47.  >have an address.  This address can only be resolved at runtime.
  48.  
  49. False. Static addresses can be nicely resolved at link time, or even compile
  50. time depending on the environment.
  51.  
  52.  >Now lets walk through a small program.  (assume for this discussion
  53.  >that we are running on a 16 bit machine, aka pc-xt.
  54.  
  55. Snore...
  56.  
  57.  >void main(void)
  58.  
  59. Slap on the wrist for void main() from me. Noose around your neck from some
  60. other regulars of c.l.c. Fix that to int main()!
  61.  
  62.  >{
  63.  >
  64.  >  int a;
  65.  >  int b[3];
  66.  >  char c[4];
  67.  >  char  *a_pointer;
  68.  >
  69.  >  a = sizeof(b);
  70.  >  a = sizeof(c);
  71.  >
  72.  >  a_pointer = c;       // or a_pointer = &c[0]
  73.  
  74. Syntax error on token //
  75.  
  76.  >  scanf("%s",c);
  77.  >
  78.  >}
  79.  >
  80.  >1.  The first four statement instructs our program to create memory
  81.  >for the indicated variables on the stack.  After these instructions
  82.  
  83. Yes, a conceptual stack.
  84.  
  85.  >are performed our memory looks like:
  86.  >
  87.  >                       address     (our name)
  88.  >________
  89.  >|                |     1000        a  (because a is an int we need 2 bytes to store it)
  90.  >|                |     1001
  91.  >-------------
  92.  >|                |     1002        b[0]  this is the first element of array b
  93.  
  94. Are you sure that the C language calls for this kind of layout? This is great
  95. from a hacker's point of view, but reveals little about the C language.
  96.  
  97.  >2.  The next thing we encounter is the sizeof instructions.  Be
  98.  >carefull here, sizeof looks like a C runtime function, acts like a C
  99.  >runtime function but is not a C runtime function.  It is really some
  100.  
  101. False! It only looks like a ``runtime function'' (whatever that is) if you
  102. insist on writing parentheses around its argument. 
  103.  
  104. It is an operator, something that is clear if you do this:
  105.  
  106.     char x[3];
  107.  
  108.     int s = sizeof x; /* don't look like no runtime function ta me! */
  109.  
  110.  >instructions to tell the compiler to calculate the size of the
  111.  >indicated variable at compile time and not at runtime.  It is because
  112.  
  113. False. Sizeof also takes parenthesized type expressions and computes their
  114. size. These are clearly not variable names or lvalues of any sort.
  115.  
  116.  >The crux of the problem.
  117.  >
  118.  >Is the name of an array a pointer?  The answer to this, using our
  119.  >definition of what a pointer is, is no.  The name of an array is not a
  120.  
  121. Right. No.
  122.  
  123.  >pointer, but it is what we would call an address.  This is because we
  124.  
  125. Wrong. It is a symbol which refers to an object. An address is but one of the
  126. many attributes that the symbol may have.
  127.  
  128. Fact: the address of an array can change. Automatic arrays will likely have a
  129. different address each time you invoke the function, and certainly so if you do
  130. it recursively. Yet, the name of the array is no longer available at run-time,
  131. so there can be no possible symbol-table association between the name and the
  132. vast multitude of possible addresses it can have.
  133.  
  134. I think you are confusing assembly language labels with C arrays.
  135.  
  136.  >define all arrays in terms of where they begin in memory.  Therefore
  137.  >the following expressions are equal
  138.  >
  139.  >          c == &c[0]
  140.  >(1008) == (1008)  ----> from our memory map
  141.  >
  142.  >This fact therefore makes it possible to intermix the following array
  143.  >notation:
  144.  >
  145.  >c[1]  or *(c + 1)
  146.  
  147. There is no logical implication here. The only ``therefore'' that is valid is
  148. one that is backed by a reading of the grammar as given in the C standard. The
  149. notation can be intermixed because the designers of the C languages decreed it
  150. so, and for no other reason!!! 
  151.  
  152.  >Missconcepetions:
  153.  >
  154.  >
  155.  >1)  Using the name of an array somehow yields or calculates or
  156.  >generates the address.
  157.  >
  158.  >This is not true. The name of the array is the address of the array.
  159.  >The operation is not a calculation or de-referencing, or ....
  160.  
  161. It is a conceptual calculation performed in the syntax directed translation of
  162. the program. The type expression (char (*)[]) calculates a type. That it's done
  163. in the compiler is irrelevant.
  164.  
  165. What makes you sure that the address of an array is not computed? If your array
  166. is known as a stack-frame offset, sure as hell you have to calculate it. The
  167. machine code has to compute the effective address by adding the displacement to
  168. the value of the stack pointer (on a machine that has this sort of familiar
  169. layout).
  170.  
  171.  >I have personally never heard of this 'decay' process.  If you look
  172.  
  173. The decay process is a real fact of C, even though it may not be evident from a
  174. translation to a particular machine architecture. Some people find the name
  175. misleading, however, because it can lead beginners into believing that an
  176. actual object is being transformed via some sort of decay process.
  177.  
  178.  >3)  Pointer take on different sizes.
  179.  
  180.  >Pointers only take on a different size when going from one platform to
  181.  >another.  If you write a program and define 1000 pointers that can
  182.  
  183. You have not read the FAQ, have you?
  184. -- 
  185.  
  186.